package com.netjava.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.NumberFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.netjava.pojo.Auctioning;
import com.netjava.pojo.EndAuction;
import com.netjava.pojo.Product;
import com.netjava.service.IAuctioningService;
import com.netjava.service.IEndAuctionService;
import com.netjava.service.IProductService;

public class UpdateTimeAction extends BaseAction{
	@SuppressWarnings("unchecked")
	public ActionForward updateTime(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		response.setContentType("text/html;charset=UTF-8");
	    response.setHeader("Cache-Control", "no-cache");
	    
	    IAuctioningService auctionService=(IAuctioningService)getBean("auctioningService");
	    IProductService productService=(IProductService)getBean("productService");
	    PrintWriter out=response.getWriter();
	    Integer count=Integer.parseInt(request.getParameter("count").toString());
//	    System.out.println("count now is :"+count);
	    Integer[] auctionId=new Integer[4];
	    for(int i=0;i<4;i++){
	    
	    	String aucS=request.getParameter(("auctionId"+i)).toString();
	    	if(!aucS.equals("/")){
	    		auctionId[i]=Integer.parseInt(aucS);
	    	}
	    	else{
	    		auctionId[i]=0;
	    	}
	    	Product product=productService.getProById(auctionId[i]);
	    	//判断请求的商品是否还存在，状态是否为正在拍卖的商品
	    	if(null==product||product.getState()!=1){
	    		return null;
	    	}else{
//	    	System.out.println("auctionId[i]:"+auctionId[i]);
	    	Auctioning auctioning=auctionService.getAuctioningById(auctionId[i]);
	    	String remainTime=auctioning.getRemainTime();
/**
 * 如果正在竞拍的商品的剩余时间等于0，那么要重新查询数据库，
 * 重置auctioningList、preauctionList、endAuctionList这几个List。
 */
	    	
	    	out.write(remainTime+"-"+i+"|");
	    	
	    	}
	    }
		out.flush();
		out.close();
		return null;
	}

	//得到最新竞拍结果的消息
	public ActionForward getNews(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		response.setContentType("text/html;charset=UTF-8");
	    response.setHeader("Cache-Control", "no-cache");
	    IEndAuctionService service=(IEndAuctionService)getBean("endAuctionService");
	    PrintWriter out=response.getWriter();
	    List<EndAuction> endAucList=service.getAllEndaucByTime();
	    if(endAucList.size()>4){
	    	for(int i=4;i<endAucList.size();i++){
	    		
	    		endAucList.remove(i);
	    	}
	    	
	    }
	    for(int i=0;i<endAucList.size();i++){
	    	EndAuction endAuction=service.getEndAuctionById(endAucList.get(i).getEndAuctionId());
	    	System.out.println("..........."+endAuction.getEndTime());
	    	System.out.println("...."+endAuction.getProduct().getProductName());
	    	float save=(endAuction.getProduct().getCurrentPrice()-endAuction.getProduct().getBasePrice())*100/endAuction.getProduct().getCurrentPrice();
	    	NumberFormat nf=NumberFormat.getPercentInstance();
	    	String saveFormat=nf.format(save);
	    	System.out.print(saveFormat); 
	    	System.out.println("currentPrice:::"+endAuction.getProduct().getCurrentPrice());
	    	out.write(endAuction.getUser().getUserName()+"  "+endAuction.getEndTime()+"  "+endAuction.getProduct().getProductName()+"  "+endAuction.getProduct().getCurrentPrice()+"  "+saveFormat+"|");
	    }
	    
		
		out.flush();
		out.close();
		return null;
	}
	

	

}
